package com.pzr.soccer.controller;

import com.pzr.soccer.constans.Consts;
import com.pzr.soccer.io.email.MailService;
import com.pzr.soccer.io.email.ToEmail;
import com.pzr.soccer.io.phone.SendPhoneText;
import com.pzr.soccer.model.R;
import com.pzr.soccer.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.concurrent.TimeUnit;

/**
 * @Author pzr
 * @date:2022-01-03-8:41
 * @Description:
 **/
@RestController
@RequestMapping("/user/validation")
@Slf4j
public class ValidationController {
    @Autowired
    SendPhoneText sendPhoneText;
    @Autowired
    MailService mailService;
    @Autowired
    RedisTemplate<String ,Object> redisTemplate;
    @Autowired
    MongoOperations mongoOperations;

    @PostMapping("/phone")
    public R<String> phoneCodeValidation(@PathParam("phone") String phone) {
        User user=mongoOperations.findOne(Query.query(Criteria.where("phone").is(phone)),User.class);
        if(user==null){
            return R.fail("发送验证码失败，没有这个手机! "+phone);
        }

        int code=(int) (Math.random()*8999+1000);
        String codeStr=String.valueOf(code);
        String signature="【深圳市soccer科技有限公司】";
        String sendText="验证码是："+codeStr+",有效期两个小时，请妥善保存！"+signature;

        //暂时没有手机短信服务，作为测试总是返回1
        int resultCode=	sendPhoneText.sentMsgGbk(sendText, phone);
        //int resultCode=	1;
        if(resultCode==1) {
            redisTemplate.opsForValue().set(Consts.Redis_Phone_Code_Prefix+phone,codeStr,2*3600, TimeUnit.SECONDS);
            System.out.println("phone code: "+codeStr+"  phone:"+phone);
            return R.ok(codeStr,"generate phone code");

        }else {
            System.out.println("sendText error code: "+resultCode);
            return R.fail("generate phone code failed!");
        }

    }

    @PostMapping("/email")
    public R<String> emailCodeValidation(@PathParam("email") String email) {
        User user=mongoOperations.findOne(Query.query(Criteria.where("email").is(email)),User.class);
        if(user==null){
            return R.fail("发送邮件失败，没有这个邮箱!"+email);
        }
        int code=(int) (Math.random()*8999+1000);
        String codeStr=String.valueOf(code);
        String signature="【深圳市soccer科技有限公司】";
        String context="验证码是："+codeStr+",有效期两个小时，请妥善保存！"+signature;
        log.info(context);
        try {
            ToEmail mail=new ToEmail();
            mail.setTo(email);
            mail.setSubject("验证邮件");
            mail.setContent(context);
            mailService.sendHtmlEmail_notAsync(mail);
            redisTemplate.opsForValue().set(Consts.Redis_Email_Code_Prefix+email,codeStr,2*3600, TimeUnit.SECONDS);
            return R.ok("发送成功，请查收邮件");
        }catch (Exception e){
            return R.fail("校验邮件发送失败:"+e.getMessage());
        }
    }
}
